﻿using System;
using System.Windows.Media;

namespace DiscoveryLogic.DataServices.DataSources
{
    public class BillPayment
    {
        #region Private Variables

        private const string gs0WarningColor = "#FFFFE3A6";
        private const string gs1WarningColor = "#FFFF9D66";
        private const string gs2WarningColor = "#FFD00000";
        private const string gs3WarningColor = "#FFFF6868";
        private const string gs4WarningColor = "#FFFFE8B6";

        private const string gs0NormalColor = "#FFC0DEF4";
        private const string gs1NormalColor = "#FFA0CAEB";
        private const string gs2NormalColor = "#FF83B7E2";
        private const string gs3NormalColor = "#FF65A3D8";
        private const string gs4NormalColor = "#FFC0DEF4";
               
        private double payment;
        private DateTime date;
        private string fromAcc;
        private BillPaymentHistory billPaymentHistory;

        #endregion

        #region Public Properties

        public double Payment
        {
            get
            {
                return payment;
            }
            set
            {
                payment = value;
            }
        }

        public DateTime Date
        {
            get
            {
                return date;
            }
            set
            {
                date = value;
            }
        }

        public string FromAcc
        {
            get
            {
                return fromAcc;
            }
            set
            {
                fromAcc = value;
            }
        }

        public BillPaymentHistory BillPaymentHistory
        {
            set
            {
                billPaymentHistory = value;
            }
        }
        
        public double BarHeight
        {
            get
            {
                return (Payment / MaxPayment) * BillPaymentHistory.MAX;
            }
        }

        public double BarTop
        {
            get
            {
                return BillPaymentHistory.MAX - BarHeight;
            }
        }

        public Color GS0Color
        {
            get
            {
                return GetGradientColor(gs0NormalColor, gs0WarningColor);                
            }
        }

        public Color GS1Color
        {
            get
            {
                return GetGradientColor(gs1NormalColor, gs1WarningColor);
            }
        }

        public Color GS2Color
        {
            get
            {
                return GetGradientColor(gs2NormalColor, gs2WarningColor);
            }
        }

        public Color GS3Color
        {
            get
            {
                return GetGradientColor(gs3NormalColor, gs3WarningColor);
            }
        }

        public Color GS4Color
        {
            get
            {
                return GetGradientColor(gs4NormalColor, gs4WarningColor);
            }
        }

        #endregion

        #region Private Properties

        private bool IsWarningBar
        {
            get
            {
                return
                    ((Payment > billPaymentHistory.AveragePayment * (1 + BillPaymentHistory.FLAG_PAYMENT_PERCENT))
                        || (Payment < billPaymentHistory.AveragePayment - (billPaymentHistory.AveragePayment * BillPaymentHistory.FLAG_PAYMENT_PERCENT)));
            }
        }


        private double MaxPayment
        {
            get
            {
                double maxPayment = Payment;

                if (billPaymentHistory != null)
                {
                    maxPayment = billPaymentHistory.MaxPayment;
                }
                return maxPayment;
            }
        }

        #endregion

        #region Private Methods
        
        private static Color HexToColor(string hex)
        {
            string a = hex.Substring(1, 2);
            string r = hex.Substring(3, 2);
            string g = hex.Substring(5, 2);
            string b = hex.Substring(7, 2);

            Color c = new Color();
            c.A = Convert.ToByte(a, 16);
            c.R = Convert.ToByte(r, 16);
            c.G = Convert.ToByte(g, 16);
            c.B = Convert.ToByte(b, 16);

            return c;
        }

        private Color GetGradientColor(string NormalColor, string WarningColor)
        {
            string color = NormalColor;
            if (IsWarningBar)
            {
                color = WarningColor;
            }
            return HexToColor(color);
        }

        #endregion

        #region Constructors


        public BillPayment()
        {
        }

        public BillPayment(double PaymentAmnt, DateTime DatePaid, string FromWhichAccount)
        {
            payment = PaymentAmnt;
            date = DatePaid;
            fromAcc = FromWhichAccount;
        }

        #endregion

        #region Public Methods

        public double getPaymentAmount()
        {
            return Payment;
        }

        public DateTime getDatePaid()
        {
            return Date;
        }

        public string getFromAcc()
        {
            return FromAcc;
        }

        #endregion
    }
}